//
//  WQToolBarView.swift
//  WeiBo
//
//  Created by 张德华 on 7/26/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

enum WQToolBarViewType: Int {
    case Picture = 101
    case Metion = 102
    case Trend = 103
    case Emoticon = 104
    case Add = 105
}

class WQToolBarView: UIView {

    
    var closure: ((type: WQToolBarViewType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    
    func setupUI() {
        
        backgroundColor = RandomColor()
        
        let pictureButton = addChildButton("compose_toolbar_picture", type: .Picture)
        let metionButton = addChildButton("compose_mentionbutton_background", type: .Metion)
        let trendButton = addChildButton("compose_trendbutton_background", type: .Trend)
        let emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        let composeButton = addChildButton("compose_add_background", type: .Add)
        
        
        pictureButton.snp_makeConstraints { (make) in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(metionButton)
        }
        metionButton.snp_makeConstraints { (make) in
            make.leading.equalTo(pictureButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.leading.equalTo(metionButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton)
        }
        emoticonButton.snp_makeConstraints { (make) in
            make.leading.equalTo(trendButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(composeButton)
        }
        composeButton.snp_makeConstraints { (make) in
            make.leading.equalTo(emoticonButton.snp_trailing)
            make.top.bottom.trailing.equalTo(self)
        }
        
    }
    
    
    
    
    func  addChildButton(imageName: String, type: WQToolBarViewType) -> UIButton {
        
        let button: UIButton = UIButton()
        
        button.tag = type.rawValue
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: #selector(WQToolBarView.toolButtonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        addSubview(button)
        return button
    }

    // MARK: - ************按钮监听事件
    func toolButtonClicked(button: UIButton) {
        closure!(type: WQToolBarViewType(rawValue: button.tag)!)
    }
    
    
}
